package wcpackage.webapplication.view.users;

import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.IFilterStateLocator;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import wcpackage.domain.entities.User;
import wcpackage.service.UserService;

import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: rabbit
 * Date: 14.03.11
 * Time: 12:55
 * To change this template use File | Settings | File Templates.
 */
public class UsersProvider extends SortableDataProvider<User> implements IFilterStateLocator<User> {

    private UserService userService;

    private User filterState;

    public UsersProvider(UserService userService) {
        this.userService = userService;
        filterState = new User();
        setSort("login", true);
    }

    public Iterator<? extends User> iterator(int first, int count) {

        System.out.println("filter: " + filterState.toString());

        String sortType = getSort().isAscending() ? "asc" : "desc";
        return userService.find(first, count, getSort().getProperty(), sortType).iterator();
    }

    public int size() {
        return userService.numberOfUsers();
    }

    public IModel<User> model(User object) {
        return new Model(object);
    }

    public void detach() {

    }

    public User getFilterState() {
        return filterState;
    }

    public void setFilterState(User state) {
        this.filterState = state;
    }
}
